﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using Gin;
using Gin.Commands;
using Gin.Controls;

namespace GinBuilder
{
    public class MainFormController
    {

        private Control control;

        public void Init(Control argumentsPanel)
        {
            control = argumentsPanel;
            GinMetaData metaData = GinMetaData.GetInstance();
            string exePath = Application.ExecutablePath;
            string workPath = Path.GetDirectoryName(exePath);
            string pluginPath = Path.Combine(workPath, @"Plugins");
            metaData.Plugin(pluginPath);
            GinSerializer.IncludeTypes(metaData.IncludedTypes);
        }

        public void CreatePackage(string packageFilePath)
        {
            GinMetaData metaData = GinMetaData.GetInstance();
            ExternalCommand iisRestart = metaData.GetCommandByName("IISRestartCommand");
            iisRestart.SetProperty("ServerName", "SERVIP");

            PackageBody body = new PackageBody()
            {
                Command = new CommandSequence()
                {
                    Commands = new List<Command>
                    {
                        new SaveConstant()
                        {
                            ResultName = "conditionValue",
                            Value = true
                        },
                        new UserInput()
                        {
                             FormCaption = "Куда устанавливать будем?",
                             InputControls = new List<UserInputControl>()
                             {
                                 new UserInputBrowseDirectoryDialog()
                                 {
                                     Caption = "Выберите папку",
                                     InitialValue = @"c:\",
                                     ResultName = "APP_DIRECTORY"
                                 },
                                 new UserInputOpenFileDialog()
                                 {
                                     Caption = "Выберите файло",
                                     InitialValue = @"d:\",
                                     ResultName = "APP_FILE_OPEN"
                                 },
                                 new UserInputSaveFileDialog()
                                 {
                                     Caption = "Выберите еще файло",
                                     InitialValue = @"g:\",
                                     ResultName = "APP_FILE_SAVE"
                                 }
                             }
                        },
                        new  TransactionContainer()
                        { 
                             TransactionName = "fileTransaction1",
                             Command = new CommandSequence()
                             {
                                 Commands = new List<Command>()
                                 {
                                     new CreateFolder()
                                     {
                                       DestPath = @"%APP_DIRECTORY%\folder1"
                                     },
                                     new CreateFile()
                                     {
                                        SourcePath = @"D:\test\newfile.txt",
                                        DestPath = @"%APP_DIRECTORY%\folder1\file3.txt"
                                     },
                                     new ExecuteIf() 
                                     {
                                        ArgumentName = "conditionValue",
                                        Then = new CommandSequence()
                                        {
                                            Commands = new List<Command>()
                                            {
                                                new CreateFile()
                                                {
                                                    SourcePath = @"D:\test\newfile.txt",
                                                    DestPath = @"%APP_DIRECTORY%\folder1\file1.txt"
                                                },
                                                new CreateFile()
                                                {
                                                    SourcePath = @"D:\test\newfile2.txt",
                                                    DestPath = @"%APP_DIRECTORY%\folder1\file2.txt"
                                                }
                                            }
                                        },
                                        Else = new ShowMessage()
                                        {
                                            MessageText = "не выполнилось"
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            };

            PackageBuilder builder = new PackageBuilder(body);
            builder.SaveResult(packageFilePath);
        }

        public void ExecutePackage(string packageFilePath)
        {
            ExecutionContext context = new ExecutionContext();
            context.ControlContainer = control;
            Package package = new Package(packageFilePath, context);
            package.Execute();
        }

        public void RollbackPackage(string packageFilePath)
        {
            ExecutionContext context = new ExecutionContext();
            Package pkg = new Package(packageFilePath, context);
            pkg.Rollback("fileTransaction1");
        }
    }
}
